package com.paas.form.core.model;

import com.gencode.util.CodeBuildNew;
import com.paas.common.util.StringUtil;
import com.paas.form.core.element.field.FieLdsModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@EqualsAndHashCode(callSuper = true)
@Data
public class ElInput extends ElementUI {

    private String icon;

    private String name;

    private String label;

    private String _maxlength;

    private String _placeholder;

    private String _v_model;

    private String event_click;

    public ElInput(String prefixName, FieLdsModel fieLdsModel, int indentNum) {
        if(fieLdsModel.getMaxlength() != null){
            this._maxlength = fieLdsModel.getMaxlength().toString();
        }
        this._placeholder = fieLdsModel.getPlaceholder();
        this._v_model = StringUtil.isNotEmpty(prefixName) ? prefixName + "." + fieLdsModel.getVModel() : fieLdsModel.getVModel();
        this.indentNum = indentNum;
    }

    public static final String VM_FORM_INPUT_HTMl = "<el-input v-model=\"${formDataModel.formModel}.${v_model}\" placeholder=\"${placeholder}\" maxlength=\"${maxlength}\" style=\"${css_style}\" class=\"${css_class}\" @click=\"${event_click}\"  clearable />";


    @Override
    public void getElHtml(StringBuilder str) {
        String codeTemplate = String.join("", Collections.nCopies(indentNum, "\t")) + VM_FORM_INPUT_HTMl;

        Map<String, Object> params = new HashMap<>();
        params.put("v_model", StringUtil.isNotEmpty(this._v_model) ?  _v_model : " ");
        params.put("placeholder", StringUtil.isNotEmpty(this._placeholder) ?  this._placeholder : " ");
        params.put("maxlength", StringUtil.isNotEmpty(this._maxlength) ?  this._maxlength  : " ");
        params.put("css_style", StringUtil.isNotEmpty(this.css_style) ?  this.css_style  : " ");
        params.put("css_class", StringUtil.isNotEmpty(this.css_class) ? this.css_class : " ");
        params.put("event_click", StringUtil.isNotEmpty(this.event_click) ?  this.event_click  : " ");


        String newStr = CodeBuildNew.getCodeString(params,codeTemplate);
        str.append(newStr);
        str.append("\n");
    }

    @Override
    public void getStartTagElHtml(StringBuilder str) {
    }

    @Override
    public void getEndTagElHtml(StringBuilder str) {
    }


}